package com.anjubao.parking.municipal.parkinglot.controller;

import com.alibaba.fastjson.JSON;
import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.BillRuleDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotBillRuleDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotBillRuleReturnCodeDto;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotBillRuleForm;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotBillRule;
import com.anjubao.parking.municipal.parkinglot.service.ParkingLotBillRuleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 停车场计费规则API
 *
 * @author zhu.chunfeng
 */
@RestController
@RequestMapping("/parking-lots-bill-rules")
@Api(value = "ParkingLotBillRuleController", tags = {"停车场计费规则管理API"})
@Slf4j
public class ParkingLotBillRuleController {

    @Resource
    private ParkingLotBillRuleService parkingLotBillRuleService;

    /**新增停车场计费规则*/
    @ApiOperation(value = "2.3.1创建停车场计费规则")
    @PostMapping("")
    public ApiResult<ParkingLotBillRuleReturnCodeDto> createParkingLotBillRule(@RequestBody @Validated ParkingLotBillRuleForm form, BindingResult result) {

        if(result.hasErrors()){
            List<ObjectError> ls=result.getAllErrors();
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), String.valueOf(ls.get(0)));
        }

        log.info("creating new parking lot {} ", JSON.toJSONString(form));
        ParkingLotBillRule rule = parkingLotBillRuleService.createParkingLotBillRule(form);
        form.getBillRuleSub().validate();
        ParkingLotBillRuleReturnCodeDto parkingLotBillRuleReturnCodeDto = new ParkingLotBillRuleReturnCodeDto();
        parkingLotBillRuleReturnCodeDto.setParkingLotBillRuleReturnCode(rule.getCode());
        return ApiResult.success(parkingLotBillRuleReturnCodeDto);
    }

    /**更改停车场计费规则*/
    @ApiOperation("2.3.2更改停车场计费规则")
    @PutMapping("/{parkingLotBillRuleCode}")
    public ApiResult<ParkingLotBillRuleReturnCodeDto> updateParkingLotBillRule(@ApiParam("计费规则code") @PathVariable String parkingLotBillRuleCode, @RequestBody ParkingLotBillRuleForm entity) {
        parkingLotBillRuleService.updateParkingLotBillRule(parkingLotBillRuleCode, entity);
        ParkingLotBillRuleReturnCodeDto parkingLotBillRuleReturnCodeDto = new ParkingLotBillRuleReturnCodeDto();
        parkingLotBillRuleReturnCodeDto.setParkingLotBillRuleReturnCode(parkingLotBillRuleCode);
        return ApiResult.success(parkingLotBillRuleReturnCodeDto);
    }

    /**查询停车场计费规则*/
    @ApiOperation("2.3.3查询停车场计费规则详情")
    @GetMapping("/{parkingLotBillRuleCode}")
    public ApiResult<ParkingLotBillRuleDto> getParkingLotBillRule(@ApiParam("计费规则code")@PathVariable String parkingLotBillRuleCode) {
        return ApiResult.success(parkingLotBillRuleService.getParkingLotBillRule(parkingLotBillRuleCode));
    }

    /** 查询停车场计费规则列表*/

    @ApiOperation("2.3.4停车场计费规则列表查询")
    @GetMapping("")
    public ApiResult<List<BillRuleDto>> getParkingLotBillRulePages(@ApiParam("计费规则名称")@RequestParam(value = "name",required = false) String name,
                                                                             @ApiParam("默认10")@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                                             @ApiParam("默认1")@RequestParam(value = "page",defaultValue = "1") Integer page) {
        IPage<BillRuleDto> parkingLotBillRulePages = parkingLotBillRuleService.getParkingLotBillRulePages(name, page, pageSize);
        return ApiResult.success(parkingLotBillRulePages.getRecords(), (int) parkingLotBillRulePages.getTotal());
    }

    /**删除停车场计费规则*/
    @ApiOperation("2.3.5删除停车场计费规则")
    @DeleteMapping("/{parkingLotBillRuleCode}")
    public ApiResult deleteParkingLotBillRule(@PathVariable String parkingLotBillRuleCode) {
        log.info("删除计费规则=====================>"+parkingLotBillRuleCode);
        parkingLotBillRuleService.deleteParkingLotBillRule(parkingLotBillRuleCode);
        return ApiResult.success();
    }

}